﻿// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Windows.Interop;

using ICSharpCode.Core;
using ICSharpCode.Core.WinForms;
using ICSharpCode.SharpDevelop.Gui;

namespace ICSharpCode.SharpDevelop.Commands
{
	/// <summary>
	/// Runs workbench initialization.
	/// Is called by ICSharpCode.SharpDevelop.Sda and should not be called manually!
	/// </summary>
	public class WorkbenchStartup
	{
		const string workbenchMemento = "WorkbenchMemento";
		App app;
		
		public void InitializeWorkbench()
		{
			app = new App();
			System.Windows.Forms.Integration.WindowsFormsHost.EnableWindowsFormsInterop();
			ComponentDispatcher.ThreadIdle -= ComponentDispatcher_ThreadIdle; // ensure we don't register twice
			ComponentDispatcher.ThreadIdle += ComponentDispatcher_ThreadIdle;
			WorkbenchSingleton.InitializeWorkbench(new WpfWorkbench(), new AvalonDockLayout());
		}
		
		static void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
		{
			System.Windows.Forms.Application.RaiseIdle(e);
		}

        public void Run()
        {
            bool didLoadSolutionOrFile = false;

            if (!didLoadSolutionOrFile)
            {
                foreach (ICommand command in AddInTree.BuildItems<ICommand>("/Workspace/AutostartNothingLoaded", null, false))
                {
                    try
                    {
                        command.Run();
                    }
                    catch (Exception ex)
                    {
                        MessageService.ShowException(ex);
                    }
                }
            }

            // finally run the workbench window ...
            app.Run(WorkbenchSingleton.MainWindow);

            // save the workbench memento in the ide properties
            try
            {
                PropertyService.Set(workbenchMemento, WorkbenchSingleton.Workbench.CreateMemento());
            }
            catch (Exception e)
            {
                MessageService.ShowException(e, "Exception while saving workbench state.");
            }
        }
	}
}
